/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.chiefmedicalofficer.mcd.model;

import com.hp.hpl.jena.vocabulary.DC;
import com.hp.hpl.jena.vocabulary.RDFS;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author twilson
 */
public class PageDescription {

    private static Logger log = Logger.getLogger(PageDescription.class);
    private GraphStore graphStore;
    private String pageUri;
    private String classUri;
    /*
     * property RDFS:label
     */
    private String label;
    /*
     * property DC:title
     */
    private String title;
    /**
     * property DC:description
     */
    private String textDescription;
    private String rdfDescription;
    private List<PropertyDescription> objectProperties;
    private List<PredicateDescription> predicates;

    public PageDescription() {
        graphStore = GraphStore.getInstance();
    }

    public PageDescription(String pageUri) {
        graphStore = GraphStore.getInstance();
        setPageUri(pageUri);
        GraphStore model = GraphStore.getInstance();
        setRdfDescription(model.describeRDF(pageUri));
        if (getRdfDescription() == null ||
                !getRdfDescription().contains(pageUri)) {
            // page is not in the data store
        } else {
            // load attributes, predicates, and properties
            setTitle(model.getAttribute(pageUri,
                    DC.title.getURI()));
            setLabel(model.getAttribute(pageUri,
                    RDFS.label.getURI()));
            setTextDescription(model.getAttribute(pageUri,
                    DC.description.getURI()));
            setPredicates(model.getPredicates(getClassUri()));
            setObjectProperties(model.getObjectProperties(pageUri));
        }
    }

    public void addObjectProperty(PropertyDescription prop) {
        if (getObjectProperties() == null) {
            setObjectProperties(new ArrayList<PropertyDescription>());
            if (prop.getObjectUri() == null) {
                // error condition!!!
                return;
            }
            prop.setSubjectUri(getPageUri());
            graphStore.addResourceTriple(getPageUri(),
                    prop.getPredicate().getUri(), prop.getObjectUri());
            getObjectProperties().add(prop);
        }
    }

    /**
     * Write a page to the graph store.
     * @param page
     */
    public void persist() {
        if (getLabel() != null
                && getLabel().trim().length() > 0) {
            graphStore.setAttribute(getPageUri(), RDFS.label.getURI(), getLabel());
        }
        if (getTitle() != null
                && getTitle().trim().length() > 0) {
            graphStore.setAttribute(getPageUri(), DC.title.getURI(), getTitle());
        }
        if (getObjectProperties() != null) {
            for (PropertyDescription objectProperty : getObjectProperties()) {
                objectProperty.persist();
            }
        }


    }

    public String getPageUri() {
        return pageUri;
    }

    public void setPageUri(String pageUri) {
        this.pageUri = pageUri;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<PropertyDescription> getObjectProperties() {
        return objectProperties;
    }

    private void setObjectProperties(List<PropertyDescription> objectProperties) {
        this.objectProperties = objectProperties;
    }

    public List<PredicateDescription> getPredicates() {
        return predicates;
    }

    private void setPredicates(List<PredicateDescription> predicates) {
        this.predicates = predicates;
    }

    public String getTextDescription() {
        return textDescription;
    }

    public void setTextDescription(String textDescription) {
        this.textDescription = textDescription;
    }

    public String getRdfDescription() {
        return rdfDescription;
    }

    public void setRdfDescription(String rdfDescription) {
        this.rdfDescription = rdfDescription;
    }

    public String getClassUri() {
        return classUri;
    }

    public void setClassUri(String classUri) {
        this.classUri = classUri;
    }

}
